package com.xiashitech.apiservice.util;

import java.time.Duration;
import java.time.LocalDateTime;

public class DateUtil {
    enum Unit {
        NANOS,MILLIS,SECONDS,MINUTES,HOURS,DAYS
    }
    public static long secondsBetween(LocalDateTime startTime, LocalDateTime endTime) {
        return dateBetween(startTime,endTime,Unit.SECONDS);
    }
    public static long minutesBetween(LocalDateTime startTime, LocalDateTime endTime) {
        return dateBetween(startTime,endTime,Unit.MINUTES);
    }
    /**
     * 获取两个时间之间的差值，返回值的单位为秒。
     * @param startTime
     * @param endTime
     * @return
     */
    public static long dateBetween(LocalDateTime startTime, LocalDateTime endTime, Unit unit) {
        Duration duration= Duration.between(startTime, endTime );
        if (unit == Unit.NANOS) {
            return duration.toNanos();
        }
        if (unit == Unit.MILLIS) {
            return duration.toMillis();
        }
        if (unit == Unit.SECONDS) {
            return duration.toMillis()/1000;
        }
        if (unit == Unit.MINUTES) {
            return duration.toMinutes();
        }
        if (unit == Unit.HOURS) {
            return duration.toHours();
        }
        if (unit == Unit.DAYS) {
            return duration.toDays();
        }
        return -1l;
    }
}
